<template>
	<div>
		<div class="box">
			<div class="title">合同</div>
			<uni-forms
				ref="form"
				:model="leaseContract"
				validate-trigger="bind"
				:label-position="state == 'write' ? 'top' : 'left'"
				label-width="auto">
				<FromItem
					label="合同模板方案"
					:required="true"
					:state="state"
					name="contractTemplateSchemeType">
					<view v-if="state == 'readonly'">
						{{
							A_review({
								value: leaseContract.contractTemplateSchemeType,
								dict: {
									list: dicts.contract,
								},
							})
						}}
					</view>
					<div v-else>
						<ARadioGroup
							v-model="leaseContract.contractTemplateSchemeType"
							:dict="{
								list: dicts.contract,
							}"></ARadioGroup>
					</div>
				</FromItem>
				<FromItem
					label="租赁合同模板"
					:required="true"
					:state="state"
					name="contractTemplateId">
					<ASelectUni
						:state="state"
						v-model="leaseContract.contractTemplateId"
						:dict="{
							list: dicts.leaseContractList,
						}"
						@change="change_leaseContract"></ASelectUni>
				</FromItem>
			</uni-forms>
			<uni-forms
				v-if="details.rentType_raw == '2'"
				ref="form1"
				:model="salesContract"
				validate-trigger="bind"
				:label-position="state == 'write' ? 'top' : 'left'"
				label-width="auto">
				<FromItem
					label="买卖合同模板"
					:required="true"
					:state="state"
					name="contractTemplateId">
					<ASelectUni
						:state="state"
						v-model="salesContract.contractTemplateId"
						:dict="{
							list: dicts.salesContractList,
							label: 'templateName',
							value: 'id',
						}"
						@change="change_salesContract"></ASelectUni>
				</FromItem>
			</uni-forms>
			<uni-forms
				ref="form2"
				:model="guaranteeContract"
				validate-trigger="bind"
				:label-position="state == 'write' ? 'top' : 'left'"
				label-width="auto">
				<FromItem
					label="担保合同模板"
					:required="details.sourceBusinessType == '2' ? true : false"
					:state="state"
					name="contractTemplateId">
					<ASelectUni
						:state="state"
						v-model="guaranteeContract.contractTemplateId"
						:dict="{
							list: dicts.guaranteeContractList,
							label: 'templateName',
							value: 'id',
						}"
						@change="change_guaranteeContract"></ASelectUni>
				</FromItem>
			</uni-forms>
		</div>
	</div>
</template>

<script>
	import * as app from '@/api/index.js';
	import * as apis from '@/api/quota/apply/api.js';
	import FromItem from '../formItem/index.vue';
	export default {
		name: '',
		components: { FromItem },
		dicts: [],
		props: {},
		inject: [
			'details_provide',
			'config_provide',
			'deptList_provide',
			'state_provide',
		],
		data() {
			return {
				dicts: {
					contract: [
						{
							label: '现有生效模板',
							value: '2',
						},
					],
					leaseContractList: [],
					salesContractList: [],
					guaranteeContractList: [],
				},
			};
		},
		computed: {
			details() {
				return this.details_provide();
			},
			config() {
				return this.config_provide();
			},
			deptList() {
				return this.deptList_provide();
			},
			state() {
				return this.state_provide();
			},
			customerId() {
				return this.details.leaseeId;
			},
			planID() {
				return this.details.schemeId;
			},
			newOrOldCars() {
				return this.details.carType;
			},
			customerTypeCode() {
				return this.details.customerTypeCode;
			},
			leaseContract() {
				return this.details.leaseContract ?? {};
			},
			salesContract() {
				return this.details.salesContract ?? {};
			},
			guaranteeContract() {
				return this.details.guaranteeContract ?? {};
			},
		},
		watch: {
			'details.leaseeId': {
				handler: function (val) {
					if (this.Empty(val)) {
						this.dicts.contractList = [];
					} else {
						this.getContractList();
					}
				},
				immediate: true,
			},
		},
		filters: {},
		created() {},
		mounted() {},
		methods: {
			change_leaseContract({ val, item }) {
				this.$set(
					this.leaseContract,
					'contractTemplateCode',
					item.templateCode,
				);
				this.$set(
					this.leaseContract,
					'contractTemplateName',
					item.label,
				);
				this.$forceUpdate();
			},
			change_salesContract({ val, item }) {
				this.$set(this.details, 'salesContract', {});
				this.$set(this.salesContract, 'contractTemplateId', item.id);
				this.$set(this.salesContract, 'contractTemplateType', '2');
				this.$set(
					this.salesContract,
					'contractTemplateSchemeType',
					this.leaseContract.contractTemplateSchemeType,
				);
				this.$set(
					this.salesContract,
					'contractTemplateCode',
					item.templateCode,
				);
				this.$set(
					this.salesContract,
					'contractTemplateName',
					item.templateName,
				);
				if (this.Empty(item.id)) {
					this.$set(this.details, 'salesContract', null);
				}
			},
			change_guaranteeContract({ val, item }) {
				this.$set(this.details, 'guaranteeContract', {});
				this.$set(
					this.guaranteeContract,
					'contractTemplateId',
					item.id,
				);
				this.$set(this.guaranteeContract, 'contractTemplateType', '3');
				this.$set(
					this.guaranteeContract,
					'contractTemplateSchemeType',
					this.leaseContract.contractTemplateSchemeType,
				);
				this.$set(
					this.guaranteeContract,
					'contractTemplateCode',
					item.templateCode,
				);
				this.$set(
					this.guaranteeContract,
					'contractTemplateName',
					item.templateName,
				);
				if (this.Empty(item.id)) {
					this.$set(this.details, 'guaranteeContract', null);
				}
			},
			getContractList() {
				this.getLeaseContractContractList();
				this.getOtherContractList('1');
				this.getOtherContractList('2');
			},
			getLeaseContractContractList() {
				if (!this.customerId) return;
				apis.otherOptionalTemplates({
					customerId: this.customerId,
					planId: this.planID,
					newOrOldCars: this.newOrOldCars,
					customerType: this.customerTypeCode,
				})
					.then(res => {
						let arr = [];
						res.data?.map(item => {
							arr.push(...item.options);
						});
						this.dicts.leaseContractList = arr;
					})
					.finally(() => {
						this.loading = false;
					});
			},
			getOtherContractList(templateClassification) {
				if (!this.customerId) return;
				return new Promise((resolve, reject) => {
					let params = {
						templateClassification,
						relatedCustomers: this.customerId,
					};
					app.TemplateList(params)
						.then(res => {
							if (res.code == 200) {
								if (templateClassification == '1') {
									this.dicts.salesContractList = res.data;
								}
								if (templateClassification == '2') {
									this.dicts.guaranteeContractList = res.data;
								}
							}
						})
						.finally(() => resolve());
				});
			},
		},
	};
</script>

<style lang="scss" scoped>
	.box {
		margin: 20rpx;
		padding: 20rpx;
		background-color: #fff;
		border-radius: 8rpx;
		.title {
			border-bottom: 1px solid #e9e9e9;
			font-weight: 500;
			padding-bottom: 10rpx;
			margin-bottom: 10rpx;
		}
	}
</style>
